// 深度比较 性能也非常好
// 但是现在使用 immer 的更多
import { Map, is } from 'immutable';

// import shallowEqual from "shared/shallowEqual";
// 1. 在 react/src/ReactMemo
export const REACT_MEMO_TYPE = Symbol.for('react.memo');

export function memo(type: any, compare: any) {
  const elementType = {
    $$typeof: REACT_MEMO_TYPE,
    type,
    compare: compare === undefined ? null : compare,
  };

  return elementType;
}

// 2. 在更新的阶段

// 2.1 在ReactChildFiber中创建fiber的过程中给tag赋值
// react-reconciler/src/ReactFiber
// createFiberFromElement
// createFiberFromTypeAndProps
// case REACT_MEMO_TYPE:
//   fiberTag = MemoComponent;
//   break getTag;

// 2.2 render阶段 beginWork阶段
// react-reconciler/src/ReactFiberBeginWork

// function beginWork() {
//   // 判断tag如果是memo
//   case MemoComponent: {
//     updateMemoComponent()
//   }
//   case SimpleMemoComponent: {
//    updateSimpleMemoComponent()
//   }
// }

function updateMemoComponent(
  current: any,
  workInProgress: any,
  Component: any,
  nextProps: any,
  renderLanes: any,
) {
  // isSimpleFunctionComponent
  // 如果是简单的函数组件会修改tag
  // workInProgress.tag = SimpleMemoComponent;
  const currentChild = current.child;
  const prevProps = currentChild.memoizedProps;
  // Default to shallow comparison 默认是浅比较
  let compare = Component.compare;
  compare = compare !== null ? compare : shallowEqual;
  if (compare(prevProps, nextProps) && current.ref === workInProgress.ref) {
    // return bailoutOnAlreadyFinishedWork(current, workInProgress, renderLanes);
  }
}

function updateSimpleMemoComponent(
  current: any,
  workInProgress: any,
  Component: any,
  nextProps: any,
  renderLanes: any,
) {
  const prevProps = current.memoizedProps;
  if (shallowEqual(prevProps, nextProps) && current.ref === workInProgress.ref) {
    // bailoutOnAlreadyFinishedWork()
  }
}
